package com.hcss.core.mvc.page;

import java.util.ArrayList;
import java.util.List;

import com.hcss.core.common.permissions.model.UserInfo;





/** 
* @ClassName: Pager 
* @Description: TODO 分页page对象
* @author lvfang
* @date 2013-3-27 上午9:46:51 
*  
*/ 
public class Pager {


	/**
	 * 当前页
	 */
	private int currentPage = 1;

	/**
	 * 是否是第一页
	 */
	private boolean first = true;


	/**
	 * 是否是末一页
	 */
	private boolean last = true;

	@SuppressWarnings("unused")
	private int len = 5;

	/**
	 * 下一页
	 */
	private int next = -1;

	private List<String> pageCodes;

	/**
	 * 每页显示的行数
	 */
	private int pageSize = 8;

	/**
	 * 前一页
	 */
	private int prev = -1;

	/**
	 * 当前页在数据库中的起始行
	 */
	private int startRow;

	private int endRow;
	/**
	 * 总页数
	 */
	private int totalPages;

	/**
	 * 总行数
	 */
	private int totalRows;

	/**
	 * 数据集
	 */
	
	private List<?> dataList;
	
	private String sort;
	
	private String order;
	private UserInfo loginUser;
	
	private List<?> footerList;
	
	
	
	public List<?> getFooterList() {
		return footerList;
	}

	public void setFooterList(List<?> footerList) {
		this.footerList = footerList;
	}

	/**
	 * @return the loginUser
	 */
	public UserInfo getLoginUser() {
		return loginUser;
	}

	/**
	 * @param loginUser the loginUser to set
	 */
	public void setLoginUser(UserInfo loginUser) {
		this.loginUser = loginUser;
	}
	
	/**
	 * @return the sort
	 */
	public String getSort() {
		return sort;
	}

	/**
	 * @param sort the sort to set
	 */
	public void setSort(String sort) {
		this.sort = sort;
	}

	/**
	 * @return the order
	 */
	public String getOrder() {
		return order;
	}

	/**
	 * @param order the order to set
	 */
	public void setOrder(String order) {
		this.order = order;
	}

	/**
	 * @return the dataList
	 */
	public List<?> getDataList() {
		return dataList;
	}

	/**
	 * @param dataList the dataList to set
	 */
	public void setDataList(List<?> dataList) {
		this.dataList = dataList;
	}

	/**
	 * 构造方法
	 */
	public Pager() {
	}

	/**
	 * 构造方法
	 * 
	 * @param totalRows
	 */
	public Pager(int totalRows) {
		this.totalRows = totalRows;
	}

	/**
	 * 构造方法
	 * 
	 * @param totalRows
	 * @param currentPage
	 */
	public Pager(int totalRows, int currentPage) {
		this.totalRows = totalRows;
		this.currentPage = currentPage;
	}

	/**
	 * 构造方法
	 * 
	 * @param totalRows
	 * @param currentPage
	 * @param pageSize
	 */
	public Pager(int totalRows, int currentPage, int pageSize) {
		this.totalRows = totalRows;
		this.currentPage = currentPage;
		this.pageSize = pageSize;

		refresh();
	}


	/**
	 * 当前页
	 * 
	 * @return 当前页
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 是否为第一页
	 * 
	 * @return 返回布尔值
	 */
	public boolean getFirst() {
		return first;
	}

	/**
	 * 是否为末一页
	 * 
	 * @return 返回布尔值
	 */
	public boolean getLast() {
		return last;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	/**
	 * 获取页码集合
	 * 
	 * @return 页码集合
	 */
	public List<String> getPageCodes() {
		if (this.pageCodes == null) {
			return new ArrayList<String>();
		}

		return pageCodes;
	}

	/**
	 * 分页大小
	 * 
	 * @return 分页大小
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 上一页
	 * 
	 * @return 上一页
	 */
	public int getPrev() {
		return prev;
	}

	/**
	 * 当前页在数据库中的起始行
	 * 
	 * @return 起始行
	 */
	public int getStartRow() {
		return startRow;
	}

	/**
	 * 总页数
	 * 
	 * @return 总页数
	 */
	public int getTotalPages() {
		return totalPages;
	}

	/**
	 * 总记录数
	 * 
	 * @return 总记录数
	 */
	public int getTotalRows() {
		return totalRows;
	}

	/**
	 * 重新计算
	 * 
	 * @return 返回true表示未计算，否则已计算
	 */
	public boolean refresh() {
		if (this.totalRows == 0) {
			return true;
		}

		totalPages = totalRows / pageSize;

		int mod = totalRows % pageSize;
		if (mod > 0) {
			totalPages++;
		}

		if (currentPage < 1) {
			currentPage = 1;
		}

		if (currentPage > totalPages) {
			currentPage = totalPages;
		}

		if (currentPage > 1) {
			prev = currentPage - 1;
		} else {
			prev = -1;
		}

		if (currentPage < totalPages) {
			next = currentPage + 1;
		} else {
			next = -1;
		}

		if (currentPage == 1) {
			first = true;
		} else {
			first = false;
		}

		if (currentPage == totalPages) {
			last = true;
		} else {
			last = false;
		}

		startRow = (currentPage - 1) * pageSize;

		return false;
	}

	/**
	 * 设置当前页
	 * 
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;

		refresh();
	}

	/**
	 * 设置当前页
	 * 
	 * @param currentPage
	 */
	public void setCurrentPage(String currentPage) {
		if (currentPage != null && currentPage.matches("([\\d]+)")) {
			this.currentPage = Integer.parseInt(currentPage);
		}

		refresh();
	}

	public void setLen(int len) {
		this.len = len;
	}

	/**
	 * 设置分页大小
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;

		refresh();
	}

	/**
	 * 设置分页大小
	 * 
	 * @param pageSize
	 */
	public void setStartRow(int currentPage, int pagerSize) {
		this.startRow = (currentPage - 1) * pagerSize;
		if(this.startRow > 0){
			this.startRow = this.startRow+1;
		}
		refresh();
	}

	/**
	 * 设置总行数
	 * 
	 * @param totalRows
	 */
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;

		refresh();
	}

	public int getEndRow() {
		return endRow;
	}

	public void setEndRow(int currentPage, int pagerSize) {
		this.endRow = currentPage * pagerSize;
		refresh();
	}
}
